import React, { useEffect, useState, useRef } from 'react';
import { connect } from 'umi';
import { Form, Button, Card, Input, InputNumber, Popover, notification } from 'antd';
import DrawerCondition from '@/components/DrawerCondition';
import EditorForm from '@/components/EditorForm';
import FormCondition from '@/components/FormCondition';
import FlxButton from '@/components/FlxButton'
import { InfoCircleOutlined } from '@ant-design/icons'
import { Row, Col, Tooltip, Space, } from 'antd'
const bindAdminSet = (props) => {
    const { dispatch, visible = {}, handleCallback, onClose, loading, cleanSelect } = props;
    const { show, detail = {}, selectIdList = [], type = 'one', } = visible
    const [params, setParams] = useState({
        assignAdminFlag: '0'
    })
    const [form] = Form.useForm();
    const [organizationAuthAdminList, setAuthAdminList] = useState([])
    const { assignAdminIds = '', id } = detail
    const { assignAdminFlag } = params
    // 新增修改 传id修改 不传id新增=
    const fetchRoleEdit = () => {
        form.validateFields().then((values) => {
            const { assignAdminIds, assignAdminFlag } = values
            if (type === 'one') {
                dispatch({
                    type: 'promoter/fakeSetAssignAdmin',
                    payload: {
                        id,
                        ...values,
                        assignAdminIds: assignAdminFlag === '1' ? assignAdminIds.toString() : ''
                    },
                    callback: () => {
                        onClose()
                        handleCallback()
                    }
                })
            } else {
                dispatch({
                    type: 'promoter/fakeSetAssignAdmin',
                    payload: {
                        idList: selectIdList,
                        ...values,
                        assignAdminIds: assignAdminFlag === '1' ? assignAdminIds.toString() : ''
                    },
                    callback: () => {
                        cleanSelect()
                        onClose()
                        handleCallback()
                    }
                })
            }

        });
    };
    useEffect(() => {
        if (organizationAuthAdminList.length === 0 && show) {
            const { assignAdminFlag } = detail
            setParams({ assignAdminFlag })
            dispatch({
                type: 'weCom/fetchSearchAdminAll',
                payload: {
                    page: 1,
                    limit: 9999,
                    status: '1'
                },
                callback: (e) => {
                    setAuthAdminList(e)
                }
            });
        }

    }, [show])



    const formItemLayout = {
        labelCol: {
            sm: { span: 0 },
        },
        wrapperCol: {
            sm: { span: 0 },
        },
    };
    const formItems = [
        {
            label: '是否绑定员工',
            name: 'assignAdminFlag',
            onChange: (e) => {
                form.setFieldsValue({
                    assignAdminIds: []
                })
                setParams({
                    assignAdminFlag: e.target.value
                })
            },
            type: 'radio',
            select: {
                0: '否',
                1: '是',
            },
        },
        {
            label: '员工选择',
            visible: assignAdminFlag == '1',
            name: 'assignAdminIds',
            type: 'select',
            mode: 'multiple',
            select: organizationAuthAdminList,
            fieldNames: { label: 'username', value: 'id' },
        },
        // {
        //     visible: assignAdminFlag == '1',
        //     type: 'noForm',
        //     formItem: <SetAdminList initialValues={initialValues} list={list} setList={setList}></SetAdminList>
        // },
    ];
    const modalProps = {
        title: '绑定员工',
        visible: show,
        onClose,
        footer: (
            <Button type="primary" loading={loading} onClick={fetchRoleEdit}>
                确定
            </Button>
        ),
    };

    return (
        <DrawerCondition {...modalProps}>
            <FormCondition formItemLayouts={formItemLayout} form={form} formItems={formItems} initialValues={{
                ...detail, assignAdminIds: assignAdminIds.length > 0 ? assignAdminIds.split(',').map(val => {
                    return parseInt(val)
                }) : []
            }}></FormCondition>
        </DrawerCondition>



    );
};

export default connect(({ loading }) => ({
    loading: loading.effects['promoter/fakeSetAssignAdmin'],
}))(bindAdminSet);
